package com.yang.web.featuretest.controller;

import com.yang.web.featuretest.annotation.Login;
import com.yang.web.featuretest.constants.Constants;
import com.yang.web.featuretest.service.impl.AuthService;
import com.yang.web.featuretest.util.JWTUtil;
import com.yang.web.featuretest.util.StoreUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/8/30 17:39
 */
@RestController
@RequestMapping("/JWT1")
public class JWTTestController {
    @Autowired
    AuthService authService;

    @GetMapping("/login")
    public String login(HttpServletResponse response) {
//        int size = new Random().nextInt(authService.users.size());
        String token = JWTUtil.creatToken("user", StoreUtil.getToken());
        Cookie cookie = new Cookie(Constants.getJwtProperty(),token);
        response.addCookie(cookie);

        return "/login.html";
    }

    @GetMapping("/index")
    @Login
    public String index(HttpServletResponse response){
        return "/index.html";
    }

    @Login
    @GetMapping("/limit")
    public String limit(){
        return "/limit.html";
    }




}
